home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / byt87ibm.arc / INSERT.ASM < prev    next >
Assembly Source File  |  1980-01-05  |  10KB  |  302 lines

  1. ;---------------------------------------------------------------
  2. ;           INSERT.ASM
  3. ;
  4. ;    IN MICROSOFT MACRO ASSEMBLER SOURCE CODE
  5. ;
  6. ; PURPOSE:
  7. ;    Insert data or blank columns into a text file.    Not only
  8. ;    string data, but the carriage return [CR], line feed [LF],
  9. ;    and/or form feed [FF] characters can be inserted.  Can be
  10. ;    used with the pipe or redirection function.
  11. ;
  12. ; SYNTAX: INSERT /n/ [<"data string">] [CR] [FF] [LF] /
  13. ;    Where n = column or character position to start the
  14. ;    insert (255 max).  CR = carriage return, LF = line feed,
  15. ;    and FF = form feed.
  16. ;
  17. ; EXAMPLE: INSERT /1/"NEW DATA"CRLF/
  18. ;    Would insert the words NEW DATA and then a carriage return
  19. ;    and line feed at the beginning of each line of the input
  20. ;    file.  The result would be that NEW DATA would be
  21. ;    inserted between each line of the input file.
  22. ;
  23. ; EXAMPLE: INSERT /9/CRLF/ < OLDDATA.TXT > NEWDATA.TXT
  24. ;    Would insert CR and LF at position 9 on each line of the
  25. ;    file OLDDATA.TXT and store it in a file named NEWDATA.TXT.
  26. ;
  27. ;   <C>  DEC. 1986    PAUL BAKER  CLEVELAND TN
  28. ;---------------------------------------------------------------
  29.         PAGE ,132
  30. dosint        MACRO function        ; call the DOS interrupt
  31.         MOV    AH,function    ; put function number in AH
  32.         INT    21h
  33.         ENDM
  34.  
  35.  
  36. code        SEGMENT byte public 'code'
  37.         ASSUME CS:CODE,DS:DATA,SS:STACK
  38.         ORG    0100H
  39. STRT:
  40.         PUSH    DS        ; DO HOUSEKEEPING TO
  41.         SUB    AX,AX        ;ALLOW RETURN TO DOS
  42.         PUSH    AX
  43.         MOV    AX,DATA
  44.         MOV    ES,AX        ; set ES to top of data seg.
  45.         CLD
  46.         XOR    CX,CX        ; clear CX
  47.         MOV    BX,CX        ; clear BX
  48.         MOV    SI,0080h    ; point to PSP
  49.         LODSB            ; find how many params
  50.         MOV    CL,AL        ; CL has count of params
  51.         CMP    CL,00h        ; if no params then show
  52.         JNZ    not_zero    ; use instructions.
  53.         MOV    DX,OFFSET use_msg
  54.         PUSH    ES
  55.         POP    DS        ; get local data segment.
  56.         dosint    09h        ; display help screen.
  57.         IRET            ; return to DOS.
  58. not_zero:    CMP    CL,07h        ; 7 is min. # of params.
  59.         JB    bad_param    ; if < 7 then error.
  60.         INC    SI        ; skip first space
  61.         LODSB            ; see if / is present
  62.         CMP    AL,2Fh
  63.         JNE    bad_param    ; if no / then error.
  64.         SUB    CX,0002h    ; adjust param count.
  65.         MOV    DI,OFFSET strt_data    ; set to top of buffer.
  66. get_strt:    LODSB            ; load next param.
  67.         DEC    CX
  68.         CMP    AL,2Fh        ; if / then must be end
  69.         JZ    get_insert    ; of first parameter
  70.         CMP    CX,0000h    ; 
  71.         JBE    bad_param    ; if last  param then leave.
  72.         CMP    AL,30h        ; if not a number between
  73.         JB    bad_param    ; 0 & 9 then exit
  74.         CMP    AL,39h        ; and give error 
  75.         JG    bad_param    ; message.
  76.         INC    BL        ; bump digit count.
  77.         CMP    BL,03h        ; three digits max.
  78.         JG    bad_param    ; if more than 3 then error.
  79.         SUB    AL,30h        ; convert to binary.
  80.         CALL    store_byte    ; store each digit.
  81.         JMP    get_strt    ; get next param
  82. get_insert:    MOV    DI,OFFSET insert_data    ; load top of buffer
  83.         CMP    BL,00h        ; if no first param then
  84.         JLE    bad_param    ; send error message.
  85.         MOV    BH,00h        ; BH will count insert bytes.
  86.         LODSB            ; get next param.
  87.         DEC    CX        ; reduce param count.
  88.         CMP    AL,22h        ; in quotes ?
  89.         JZ    in_quotes    ; if so then go process.
  90.         CMP    AL,2Fh        ; if another / then
  91.         JZ    bad_param    ; invalid parameter.
  92. no_quotes:    MOV    AH,AL        ; move low byte to high byte
  93.         LODSB            ; load next byte.
  94.         DEC    CX        ; reduce param count.
  95.         CALL    caps        ; force upper case.
  96.         CMP    AX,4352h    ; if CR.
  97.         JZ    cr
  98.         CMP    AX,4C46h    ; if LF.
  99.         JZ    lf
  100.         CMP    AX,4646h    ; if FF.
  101.         JZ    ff
  102.         JMP    bad_param    ; else must be error.
  103. bad_param:    MOV    DX,OFFSET err1    ; point to error message
  104.         MOV    CX,2Fh        ; send 47 bytes.
  105.         MOV    BX,0002h    ; send to error output.
  106.         PUSH    ES
  107.         POP    DS        ; get new data segment
  108.         dosint    40h        ; display it
  109. leave:    IRET        ; RETURN TO DOS
  110. cr:        MOV    AL,0Dh        ; load CR byte.
  111.         JMP    put_insert
  112. lf:        MOV    AL,0Ah        ; load LF byte.
  113.         JMP    put_insert
  114. ff:        MOV    AL,0Ch        ; load FF byte.
  115. put_insert:    CALL    store_byte    ; store insert info.
  116.         INC    BH        ; bump insert byte count.
  117.         LODSB            ; get next param.
  118.         CMP    AL,2Fh        ; if / then must be end
  119.         JZ    process     ; of parameters.
  120.         CMP    AL,22h        ; if quotes then
  121.         JZ    in_quotes    ; is in quotes again.
  122.         JMP    no_quotes    ; else loop back.
  123. in_quotes:    LODSB
  124.         CMP    AL,22h        ; if " then out of quotes.
  125.         JNE    in_quotes1    ; if not " then go on.
  126.         LODSB            ; else get next after ".
  127.         CMP    AL,2Fh        ; if / then must be end
  128.         JZ    process
  129.         JMP    no_quotes
  130. in_quotes1:    CMP    AL,2Fh        ; if / then must be error
  131.         JZ    bad_param    ; since no closing ".
  132.         CALL    store_byte    ; store current byte.
  133.         INC    BH        ; bump insert byte count.
  134.         JMP    in_quotes    ; loop back.
  135. ;
  136. ; -----------  PROCESS INCOMING DATA  -------------------
  137. ;
  138. process:    
  139.         PUSH    ES        ; load local data pointer.
  140.         POP    DS
  141.         MOV    insert_len,BH    ; store insert length.
  142.         MOV    digit_cnt,BL    ; store digit count.
  143.         MOV    AX,01h        ; start AX @ 1
  144.         XOR    CX,CX        ; clear CX register.
  145.         MOV    CL,digit_cnt    ; load loop count.
  146. loop1:         MUL    mult10        ; multiply by 10
  147.         LOOP    loop1        ; create multiplier
  148.         MOV    BX,AX        ; save multiplier.
  149.         MOV    CL,digit_cnt    ; load loop count.
  150.         MOV    SI,OFFSET strt_data    ; point to 1'st digit
  151. loop2:         MOV    AX,BX        ; get multiplier.
  152.         DIV    mult10        ;
  153.         MOV    AH,00h        ; clear remainder.
  154.         MOV    BX,AX        ; update multiplier.
  155.         MUL    BYTE PTR [SI]    ; multiply current digit.
  156.         ADD    strt_col,AL    ; update start col #.
  157.         INC    SI        ; bump pointer.
  158.         LOOP    loop2
  159. get_ready:    XOR    BX,BX        ; load handle 00
  160.         dosint    45h        ; get file duplicate.
  161.         MOV    BP,AX        ; set base pointer to handle.
  162.         dosint    3Eh        ; close file.
  163. ;        MOV    BX,0002h    ; 
  164. ;        dosint    45h        ; file duplicate.
  165. read_data:    CLD
  166.         MOV    DX,OFFSET data_buf    ; store in data_buf
  167.         MOV    CX,800h     ; set to read 800h bytes.
  168.         MOV    BX,BP        ; set BX to file handle.
  169.         dosint    3Fh        ; read input data.
  170.         OR    AX,AX        ; data read ?
  171.         JNZ    more_data
  172. no_data:    IRET
  173. more_data:    MOV    CX,AX        ; CX has count of bytes read.
  174.         MOV    SI,DX        ; point to top of data.
  175. get_byte:    MOV    BL,strt_col    ; see if this is
  176.         CMP    BL,col_cnt    ; where the insert goes.
  177.         JNZ    no_hit        ; if not, then no hit.
  178.         CALL    make_insert    ; else make insert.
  179. no_hit: LODSB            ; get first byte.
  180.         CMP    AL,1Ah        ; if end of file, quit.
  181.         JZ    no_data
  182.         CMP    AL,0Dh        ; cr ?
  183.         JNZ    no_cr        ; if not, go on.
  184.         MOV    col_cnt,01h    ; else reset column count.
  185.         MOV    DL,AL        ; send the CR
  186.         dosint    02h
  187.         DEC    CX        ; reduce byte count.
  188.         JMP    no_hit        ; get next byte.
  189. no_cr:         CMP    AL,09h        ; check for tab.
  190.         JNZ    no_tab
  191.         CALL    tab        ; if so, call tab routin.
  192.         JMP    get_byte    ; loop back.
  193. no_tab:      CMP    AL,1Fh        ; do not count anything
  194.         JBE    no_count    ; else below 20h.
  195. send_byte:    INC    col_cnt     ; bump column count.
  196. no_count:    MOV    DL,AL        ; send byte to disply.
  197.         dosint    02h
  198.         DEC    CX
  199. go_back:    JCXZ    read_data
  200.         JMP    get_byte
  201.         IRET
  202. ;
  203. ; ---------- MAKE INSERT  ROUTINE  ------------------------
  204. ;
  205. make_insert:    CMP    insert_len,01h    ; if nothing to insert
  206.         JB    exit_insert    ; then leave.
  207.         PUSH    CX        ; save CX and 
  208.         PUSH    SI        ; and SI info.
  209.         MOV    SI,OFFSET insert_data    ; point to data.
  210.         XOR    CX,CX        ; load CX with
  211.         MOV    CL,insert_len    ; loop count.
  212. insert_loop:    LODSB            ; load next byte.
  213.         MOV    DL,AL
  214.         dosint    02h        ; send byte out.
  215.         LOOP    insert_loop
  216.         POP    SI        ; restore SI and
  217.         POP    CX        ; CX data.
  218. exit_insert:    RET
  219. ;
  220. ;----------------  TAB ROUTINE    ---------------------------
  221. ;
  222. tab:        MOV    CX,0008h    ; expand tabs count.
  223.         MOV    DL,20h        ; load space byte.
  224. tab_loop:    CMP    BL,col_cnt    ; time to make insert ?
  225.         JNZ    not_yet     ;
  226.         CALL    make_insert    ; if so, then call insert.
  227.         MOV    DL,20h        ; reload space byte.
  228. not_yet:    dosint    02h        ; send byte.
  229.         INC    col_cnt     ; bump column count.
  230.         LOOP    tab_loop    ; loop back.
  231.         RET
  232. ;
  233. ; --------------- CAPS    ROUTINE  --------------------------
  234. ;
  235. caps:         CMP    AL,61h        ; convert to 
  236.         JB    exit_caps    ; all caps.
  237.         CMP    AL,7Ah
  238.         JG    exit_caps
  239.         AND    AX,5F5Fh
  240. exit_caps:    RET
  241. ;
  242. ; ----------  STORE BYTE ROUTINE  -----------------------------
  243. ;
  244. store_byte:    PUSH    DS        ; store current DS
  245.         PUSH    ES        ; local data segment info.
  246.         POP    DS        ; exchange them.
  247.         STOSB            ; store local data.
  248.         PUSH    DS
  249.         POP    ES        ; restore original
  250.         POP    DS        ; positions.
  251.         RET
  252. CODE    ENDS
  253.  
  254. ;
  255. ; ------------     STACK    SEGMENT  -----------------------------
  256. ;
  257. stack    SEGMENT STACK 'stack'
  258.         DB    32 DUP(?)
  259. stack    ENDS
  260.  
  261. ;
  262. ; -------------  DATA SEGMENT  --------------------------------
  263. ;
  264. data        SEGMENT byte public 'DATA'
  265. err1        DB  'INSERT : error - missing or invalid paramaters $'
  266. strt_data    DB    3h DUP (00h)    ; start position as entered
  267. strt_col    DB    00        ; column to start insert
  268. col_cnt DB    01h        ; current column count.
  269. digit_cnt    DB    00h        ; # of digits in 1'st param
  270. insert_len    DB    00h        ; length of insert data string
  271. insert_data    DB    80h DUP(?)    ; buffer for insert data
  272. data_buf    DB    800h DUP(?)    ; buffer for input data.
  273. mult10    DB    0Ah        ; times 10 multiplier.
  274. use_msg DB    13,10,'PURPOSE :',13,10,10
  275.   DB   '   Insert data or blank columns into a '
  276.   DB   'text file. Not only string data, but',13,10
  277.   DB   '   the Carriage Return [CR], Line Feed [LF]  and/or'
  278.   DB   ' Form Feed [FF] characters ',13,10
  279.   DB   '   can be inserted. Can be used with the pipe or '
  280.   DB   ' re-direction function.',13,10,10
  281.   DB  'SYNTAX : INSERT /n/ [<"data string">] [CR] [FF] [LF] /',13,10
  282.   DB   10,'   Where n = column or character position'
  283.   DB   ' to start the insert. (255 max.)',13,10
  284.   DB   '   CR = Carriage Return, LF = Line Feed and  FF = Form Feed'
  285.   DB   13,10,10,'EXAMPLE : INSERT /1/"NEW DATA"CRLF/',13,10,10
  286.   DB   '   Would insert the words NEW DATA and then a carriage'
  287.   DB   ' return and line feed ',13,10
  288.   DB   '   at the begining of each line of the input file.'
  289.   DB   'The result would be that',13,10
  290.   DB   '   NEW DATA would be inserted between each line'
  291.   DB   ' of the input file.',10,13,10
  292.   DB   'EXAMPLE :  INSERT /9/CRLF/ < OLDDATA.TXT > NEWDATA.TXT'
  293.   DB   13,10,10,'   Would insert CR and LF at position 9 on each'
  294.   DB   ' line of the file OLDDATA.TXT ',13,10
  295.   DB   '   and store it in a disk file'
  296.   DB   ' named NEWDATA.TXT.',13,10,10
  297.   DB   '          <C>  DEC. 1986   PAUL BAKER $'
  298. data        ENDS
  299.      END STRT
  300. isk file'
  301.   DB   ' named NEWDATA.TXT.',13,10,10
  302.   DB   '